#include "mergeSort.h"
#include <iostream>

void mergeSort(int num[], int temp[], int size)
{
	  split(num, temp, 0, size - 1);
}
 
void split(int num[], int temp[], int left, int right)
{
	int mid = (right+left)/2;
	 
	if (right > left)
	{
		split(num, temp, left, mid);
		split(num, temp, (mid+1), right);
		merge(num, temp, left, (mid+1), right);
	}
}

void merge(int num[],int temp[], int left, int mid, int right)
{
	int lend = mid -1,i = left, total = (right - left) + 1;

	while(left<=lend && mid<=right)
	{
		if(num[left] <= num[mid])
			temp[i++] = num[left++];
		else
			temp[i++] = num[mid++]; 
			
	}

	while(left<=lend)
		temp[i++] = num[left++];
	while(mid<=right)
		temp[i++] = num[mid++];

	for(int j=0;j<total;j++)
		num[right] = temp[right--];
}

int main()
{
	int a[5] = {65, 72, 105, 55, 2};
        int temp[5];
		 
	mergeSort(a, temp, 5);
			 
        for (int i = 0; i < 5; i++)
        {
		std::cout << a[i] << " ";
        }
	return 0;
}
